package com.enjoy.tx;

import com.enjoy.tx.datasource.SampleMySQLDataSource;
import com.enjoy.tx.datasource.SampleOracleDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.*;
import java.util.Random;

@Component
@SuppressWarnings("all")
public class B implements IB {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insertUseMySQL() throws SQLException {
        Connection connection = SampleMySQLDataSource.getInstance().getConnection();

        //JDBC4Connection
        System.out.println("connection = " + connection);
        String sql = "insert into t_user (id,name,age,create_time) values (?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);

        Random random = new Random();
        int id = random.nextInt(100);
        int age = random.nextInt(100);
        String name = "" + (char) random.nextInt(128);
        Date date = new Date(System.currentTimeMillis());

        statement.setInt(1, id);
        statement.setString(2, name);
        statement.setInt(3, age);
        statement.setDate(4, date);


        // statement.execute()的返回值表示有没有resultset对象
        statement.executeUpdate();


//        int a = 1 / 0;


        statement.close();
        connection.close();
    }

    @Override
    public void selectUseMySQL() throws SQLException {

        Connection connection = SampleMySQLDataSource.getInstance().getConnection();

        //JDBC4Connection
        System.out.println("connection = " + connection);

        String sql = "select * from t_user";
        PreparedStatement statement = connection.prepareStatement(sql);

        if (statement.execute(sql)) {
            ResultSet resultSet = statement.getResultSet();
            while (resultSet.next()) {
                System.out.println("id = " + resultSet.getInt("id"));
                System.out.println("name = " + resultSet.getString("name"));
                System.out.println("age = " + resultSet.getString("age"));
                System.out.println("create_time = " + resultSet.getDate("create_time"));
            }
        }

        statement.close();
        connection.close();

    }

    @Override
    public void selectUseOracle() throws SQLException {
        //T4CConnection
        Connection connection = SampleOracleDataSource.getInstance().getConnection();

        System.out.println("connection = " + connection);

        PreparedStatement statement = connection.prepareStatement("select * from mpi_property");

        ResultSet resultSet = statement.executeQuery();

        while (resultSet.next()) {

            String key = resultSet.getString("key");
            System.out.println("key = " + key);
            String value = resultSet.getString("value");
            System.out.println("value = " + value);
        }

    }

    @Override
    @Transactional
    public void insert() {

        int id = new Random().nextInt(Integer.MAX_VALUE);
        int age = new Random().nextInt(100);
        String sql = "insert into t_user (id,name,age,create_time,update_time) values (?,?,?,current_time(),current_time())";
        Object[] args = {id, "a", age};

        this.jdbcTemplate.update(sql,args);


        int a = 1 / 0;

//        sql = "select * from t_user";
//        this.jdbcTemplate.execute(sql);

    }
}
